# -*- coding: utf-8 -*-
# @Time    : 2021/10/21 11:38
# @Author  : huangwei
# @File    : img_functions.py
# @Software: PyCharm
import cv2
import numpy as np

# 去除印章
filepath = "images/simple3.png"
img = cv2.imread(filepath)  # 默认bgr通道

import colorsys

# colorsys.rgb_to_hsv()

# 分离图片的通道
blue_c, green_c, red_c = cv2.split(img)


# red_c = green_c
# cv2.imshow('blue', blue_c)
# cv2.imshow('green', green_c)
# cv2.imshow('red', red_c)

# thresh, ret = cv2.threshold(red_c, 0, 255, cv2.THRESH_OTSU)
# print("thresh:", thresh)
# cv2.imshow("ret", ret)

# filter_thresh = int(thresh * 0.8)
# print("filter thresh:", filter_thresh)

# _, red_thresh = cv2.threshold(red_c, filter_thresh, 255, cv2.THRESH_BINARY)
# cv2.imshow('red_c', red_thresh)

# new_img = np.expand_dims(red_thresh, axis=2)
# new_img = np.concatenate((new_img, new_img, new_img), axis=-1)
#
# cv2.imshow("new_img", new_img)
# cv2.waitKey(0)

def getpos( event, x, y, flags, param ):
    if event == cv2.EVENT_LBUTTONDOWN:
        print("pos:", x, y)

        bgr = img[y, x]

        print("bgr:", bgr)
        print(blue_c[y, x], green_c[y, x], red_c[y, x])

        print(bgr[-1], bgr[-2], bgr[-3])
        hsv = colorsys.rgb_to_hsv(bgr[-1], bgr[-2], bgr[-3])
        print("hsv:", hsv)

        print("h s v:", hsv[0] * 180, hsv[1] * 255, hsv[2])


cv2.imshow('img', img)
cv2.setMouseCallback('img', getpos)
cv2.waitKey(0)

# 将图片该颜色地方改为255
# w, h = img.shape[0:2]
# for m in range(w):
#     for n in range(h):
#         if img[m][n][2] > 200 and img[m][n][0] < 100 and img[m][n][1] < 100:
#             img[m][n] = [255, 255, 255]
#
# cv2.imshow('a', img)
# cv2.imwrite("images/a.png", img)
# cv2.waitKey(0)

# def remove_red_seal( input_img ):
#     # 分离图片的通道
#     blue_c, green_c, red_c = cv2.split(input_img)
#     # 利用大津法自动选择阈值
#     thresh, ret = cv2.threshold(red_c, 0, 255, cv2.THRESH_OTSU)
#     # 对阈值进行调整
#     filter_condition = int(thresh * 0.90)
#     # 移除红色的印章
#     _, red_thresh = cv2.threshold(red_c, filter_condition, 255, cv2.THRESH_BINARY)
#     # 把图片转回3通道
#     result_img = np.expand_dims(red_thresh, axis=2)
#     result_img = np.concatenate((result_img, result_img, result_img), axis=-1)
#
#     return result_img

# input_img = cv2.imread("1.jpg")
# remove_seal = remove_red_seal(input_img)
# cv2.imwrite("remove_seal.jpg", remove_seal)
